<template>
  <div class="personal-center-container">
    <h2>个人中心</h2>
    
    <div class="user-profile">
      <div class="avatar-container">
        <div class="avatar-placeholder">头像</div>
      </div>
      <div class="user-info">
        <h3>{{ userInfo.username || '未设置用户名' }}</h3>
        <p>账号: {{ userInfo.account || '未设置账号' }}</p>
      </div>
    </div>
    
    <div class="info-form">
      <div class="input-group">
        <label>用户名:</label>
        <input type="text" v-model="userInfo.username" />
      </div>
      <div class="input-group">
        <label>账号:</label>
        <input type="text" v-model="userInfo.account" />
      </div>
      <div class="input-group">
        <label>密码:</label>
        <input type="password" v-model="userInfo.password" />
      </div>
      <div class="input-group">
        <label>联系方式:</label>
        <input type="text" v-model="userInfo.contact" />
      </div>
      <div class="input-group">
        <label>邮箱:</label>
        <input type="email" v-model="userInfo.email" />
      </div>
      <div class="input-group">
        <label>收货地址:</label>
        <textarea v-model="userInfo.address" rows="3"></textarea>
      </div>
      <button class="update-button" @click="updateUserInfo">保存修改</button>
    </div>
    
    <div class="user-stats">
      <div class="stat-item">
        <h3>{{ favoriteCount }}</h3>
        <p>收藏商品</p>
      </div>
      <div class="stat-item">
        <h3>{{ orderCount }}</h3>
        <p>订单数量</p>
      </div>
      <div class="stat-item">
        <h3>{{ cartCount }}</h3>
        <p>购物车</p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'PersonalCenter',
  data() {
    return {
      userInfo: {
        username: '',
        account: '',
        password: '',
        contact: '',
        email: '',
        address: ''
      },
      favoriteCount: 0,
      orderCount: 0,
      cartCount: 0
    }
  },
  created() {
    // 从本地存储获取用户信息
    const storedUserInfo = JSON.parse(localStorage.getItem('userInfo'));
    if (storedUserInfo) {
      this.userInfo = storedUserInfo;
    }
    
    // 获取收藏、订单和购物车数量
    this.loadUserStats();
  },
  methods: {
    updateUserInfo() {
      localStorage.setItem('userInfo', JSON.stringify(this.userInfo));
      alert('个人信息已更新');
    },
    loadUserStats() {
      // 获取收藏数量
      const favorites = JSON.parse(localStorage.getItem('favorites')) || [];
      this.favoriteCount = favorites.length;
      
      // 获取订单数量 (模拟数据)
      this.orderCount = 5;
      
      // 获取购物车数量
      const cart = JSON.parse(localStorage.getItem('cart')) || [];
      this.cartCount = cart.length;
    }
  }
}
</script>

<style scoped>
.personal-center-container {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  width: 100%;
  height: 100%;
  padding: 20px;
  box-sizing: border-box;
  overflow-y: auto;
}

.user-profile {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  padding: 15px;
  background-color: #f9f9f9;
  border-radius: 8px;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
  width: 100%;
}

.avatar-container {
  margin-right: 20px;
}

.avatar-placeholder {
  width: 80px;
  height: 80px;
  background-color: #ddd;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 16px;
  color: #888;
}

.user-info h3 {
  margin: 0 0 8px 0;
  font-size: 18px;
}

.user-info p {
  margin: 0;
  color: #666;
  font-size: 14px;
}

.info-form {
  margin-bottom: 20px;
  padding: 15px;
  background-color: #f9f9f9;
  border-radius: 8px;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
  width: 100%;
}

.input-group {
  margin-bottom: 15px;
  width: 100%;
}

.input-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: bold;
}

.input-group input, .input-group textarea {
  width: 100%;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 5px;
  font-size: 14px;
}

.update-button {
  padding: 10px 20px;
  background-color: #4a90e2;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.3s ease;
  font-size: 14px;
}

.update-button:hover {
  background-color: #357ab8;
}

.user-stats {
  display: flex;
  justify-content: space-between;
  text-align: center;
  width: 100%;
}

.stat-item {
  flex: 1;
  padding: 12px;
  background-color: #f9f9f9;
  border-radius: 8px;
  margin: 0 5px;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

.stat-item:first-child {
  margin-left: 0;
}

.stat-item:last-child {
  margin-right: 0;
}

.stat-item h3 {
  margin: 0;
  font-size: 20px;
  color: #4a90e2;
}

.stat-item p {
  margin: 5px 0 0 0;
  color: #666;
  font-size: 14px;
}

@media (max-width: 768px) {
  .user-profile {
    flex-direction: column;
    text-align: center;
  }
  
  .avatar-container {
    margin-right: 0;
    margin-bottom: 15px;
  }
  
  .user-stats {
    flex-direction: column;
  }
  
  .stat-item {
    margin: 5px 0;
    width: 100%;
  }
  
  .stat-item:first-child {
    margin-top: 0;
  }
  
  .stat-item:last-child {
    margin-bottom: 0;
  }
}
</style>